/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResourceCondition extends GameResourceEx
{
	public GameResourceCondition()
	{	
	}
	
	public GameResourceCondition(Parcel in)
	{
		super(in);
	}
	
	public GameResourceCondition(int type)
	{
		super();
		
		_conditionType = type;
	}
	
	public GameResourceCondition(GameResourceCharacter character, int type)
	{
		super();
		
		setCharacter(character);
		_conditionType = type;
	}
	
	public static final Parcelable.Creator<GameResourceCondition> CREATOR = new Parcelable.Creator<GameResourceCondition>() 
	{
		public GameResourceCondition createFromParcel(Parcel in) 
		{
		    return new GameResourceCondition(in);
		}
		
		public GameResourceCondition[] newArray(int size) 
		{
		    return new GameResourceCondition[size];
		}
	};
	
	public int getConditionType()
	{
		return _conditionType;
	}
	
	public int getName()
	{
		return getFolder().getTypeConditionTitle(_conditionType);
	}
	
	public int getOrder()
	{
		return getFolder().getTypeConditionOrder(_conditionType);
	}
	
	public boolean isType(int type)
	{
		return (_conditionType == type);
	}
	
	public boolean isVisible()
	{
		return (getFolder().isConditionVisible(_conditionType));
	}
	
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		setConditionType(getJSONInt(data, "conditionType"));
	    
	    return data;
	}
	
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = new JSONObject();
	    
	    data.put("conditionType", _conditionType);
	    
	    return data;
	}
	
	public void setConditionType(int type)
	{
		_conditionType = type;
	}

	@Override
	protected void readFromParcelTransform(Parcel in) 
		throws Throwable
	{
		try
    	{
			setConditionType(in.readInt());
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransform", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransform(Parcel dest, int flags)
		throws Throwable
	{
		try
    	{
			dest.writeInt(_conditionType);
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransform", tr);
    		throw tr;
    	}
	}
	
	private int _conditionType;

	private static final String TAG = GameResourceCondition.class.getSimpleName();
}
